package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author Jonathan Chen
	 */
	public class Gug extends MovieClip
	{
		private var scoreBoard:ScoreBoard = null;
		private var deathCount:DeathCount = null;
		public var poison:MovieClip;
		public var death:MovieClip;
		private var deathTimer:Timer = new Timer(5000, 1);
		
		public function Gug(scoreBoard:ScoreBoard, deathCount:DeathCount)
		{
			this.scoreBoard = scoreBoard;
			this.deathCount = deathCount;
			addEventListener(Event.ADDED_TO_STAGE, onCreate);
			addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onCreate(evt:Event)
		{
			deathTimer.addEventListener("timer", timerHandler);
            deathTimer.start();
			
			if (Math.random() > 0.25)
				poison.visible = false;
			else
				poison.visible = true;
			
			death.visible = false;
		}
		
		private function onClick(evt:MouseEvent)
		{
			if (this.currentFrame < 2)
			{
				if(poison.visible)
					deathCount.AddCount(1);
				else
					scoreBoard.AddPoint(1);
					
				gotoAndStop(2);
			}
		}
		
		private function timerHandler(evt:Event)
		{
			if (this.currentFrame < 2)
			{
				if(!poison.visible)
					deathCount.AddCount(1);
				else
					poison.visible = false;
				
				death.visible = true;
				gotoAndStop(2);
			}
		}
	}

}